package api;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 当前就是未登录状态, 谈不上退出登录!
            resp.sendRedirect("login.html");
            return;
        }
        // 之前在登录成功后, 就会给 session 中存储 user 这样的 Attribute .
        // 把这个删掉之后, 自然就会判定为 "未登录" 了.
        session.removeAttribute("user");
        resp.sendRedirect("./html/login.html");
    }
}